package com.balabala.gym_system.config;

import com.balabala.gym_system.interceptor.MyHandlerInterceptor;
import com.balabala.gym_system.webSocket.WebSocketHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * Author:zl
 * Date:2023/5/3 11:08
 */
@Configuration
@EnableWebSocket//开启websocket
public class WebSocketConfig implements WebSocketConfigurer {
    //创建websocket处理类对象
    @Bean
    public WebSocketHandler webSocketHandler(){
        return new WebSocketHandler();
    }
    //配置拦截器
    @Bean
    public MyHandlerInterceptor myHandlerInterceptor(){
        return new MyHandlerInterceptor();
    }
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(webSocketHandler(),"/websocket/{token}").setAllowedOrigins("*").addInterceptors(myHandlerInterceptor());

    }
}
